package cn.xydpeng.springframework.beans.factory.support;

import cn.xydpeng.springframework.beans.BeansException;
import cn.xydpeng.springframework.beans.factory.config.BeanDefinition;
import cn.xydpeng.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import cn.xydpeng.springframework.util.Assert;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xydpeng
 * @version v1.0
 * @date: 2022/10/14
 * @description:
 */
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {

    /**
     * Map of bean definition objects, keyed by bean name.
     */
    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);


    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        Assert.notNull(beanName, "beanName must not be null");
        Assert.notNull(beanDefinition, "beanDefinition must not be null");
        beanDefinitionMap.put(beanName, beanDefinition);
    }

    @Override
    public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
        BeanDefinition bd = this.beanDefinitionMap.get(beanName);
        if (bd == null) {
            throw new BeansException(beanName + ":no such BeanDefinition");
        }
        return bd;
    }

    @Override
    public boolean containsBeanDefinition(String beanName) {
        return beanDefinitionMap.containsKey(beanName);
    }

    @Override
    public String[] getBeanNamesForType(Class<?> type) {
        //throw new RuntimeException("getBeanNamesForType method not be implemented");
        ArrayList<String> list = new ArrayList<>();
        this.beanDefinitionMap.forEach((beanName, mbd) -> {
            RootBeanDefinition rbd = (RootBeanDefinition) mbd;
            if (type.isAssignableFrom(rbd.getBeanClass())) {
                list.add(beanName);
            }
        });
        return list.toArray(new String[0]);
    }

    @Override
    public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
        throw new RuntimeException("getBeanNamesForAnnotation method not be implemented");
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
        throw new RuntimeException("getBeansOfType method not be implemented");
    }

    @Override
    public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException {
        throw new RuntimeException("getBeansWithAnnotation method not be implemented");
    }

    @Override
    public void preInstantiateSingletons() throws BeansException {
        List<String> beanNames = new ArrayList<>(this.beanDefinitionMap.keySet());
        // Trigger initialization of all non-lazy singleton beans...
        for (String beanName : beanNames) {
            BeanDefinition bd = getBeanDefinition(beanName);
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
                //如果是通过FactoryBean类型配置的bean,则先触发创建FactoryBean对象本身
                if(isFactoryBean(beanName)){
                    Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                }else{
                    getBean(beanName);
                }
            }
        }
    }


}
